Data preparations

# paths
current_dir <- getwd()
plot_dir <- file.path(current_dir, "plots")

# load data & functions
load("XSTSF_production.RData")
source('functions.R')
# data processing
f0_all_ct <- f0_all_pre %>% filter(focus_condition == 'ct' ) %>% 
  group_by(speaker) %>% 
  mutate(norm_f0 = scale(log(f0))) %>% 
  ungroup()

f0_di_ct_lcmh <- f0_all_ct %>% 
  filter(syntax %in% c('L', 'M') & diortri == 'di') %>% 
  mutate(sandhi_tone = case_when(sandhi_tone == 'HLLM' ~ 'HMML',
                                 sandhi_tone == 'LLHL' ~ 'LLRF', 
                                 .default = sandhi_tone)) %>% 
  filter(!ind_no %in% c('S2_1_ct', 'S2_11_ct', 'S2_27_ct', 'S3_5_ct', 'S3_19_ct', 'S5_27_ct')) %>% 
  filter(is.na(sandhi_tone) == FALSE) 
  
f0_di_ct_lcmh_h <- f0_di_ct_lcmh %>% filter( grepl('^H', sync_tone1))
f0_di_ct_lc_h <- f0_di_ct_lcmh_h %>% filter(syntax == 'L')
f0_di_ct_mh_h <- f0_di_ct_lcmh_h %>% filter(syntax == 'M')
f0_di_ct_lcmh_l <- f0_di_ct_lcmh %>% filter( grepl('^[LR]', sync_tone1)) 

f0_di_ct_lcmh_h <- f0_di_ct_lcmh_h %>% 
  mutate(sync_tone1 = ifelse(sync_tone1 == 'RF', 'LHL', sync_tone1),
         sync_tone2 = ifelse(sync_tone2 == 'RF', 'LHL', sync_tone2)) %>% 
  mutate(sync_tone1 = factor(sync_tone1, levels = c('HH', 'HL', 'LH', 'LHL')),
         sync_tone2 = factor(sync_tone2, levels = c('HH', 'HL', 'LH', 'LHL'))) %>% 
  mutate(hist_tone1 = factor(hist_tone1, levels = c('yinping', 'yinshang')),
         hist_tone2 = factor(hist_tone2, levels = c('yinping', 'yinqu', 'yangping', 'yangqu'))) %>%
  mutate(sandhi_tone = factor(sandhi_tone, levels = c('HMML', 'MMMH', 'MHHL', 'HHHH')))

Example

test <- f0_di_ct_lcmh_h %>% filter(speaker %in% c('S1', 'S4', 'S6', 'S8'),
                                   hist_tone1 == 'yinshang')

ggplotly(draw_by(test, 'speaker'), tooltip = c('text', 'x'))

combine plots

plot1 <- magick::image_read(file.path(plot_dir, "plotly_holistic.png"))
plot2 <- magick::image_read(file.path(plot_dir, "plotly_individual.png"))
plot3 <- magick::image_read(file.path(plot_dir, "plotly_inter.png"))

# Convert magick images to cowplot-compatible plots
img1 <- ggdraw() + draw_image(plot1)
img2 <- ggdraw() + draw_image(plot2)
img3 <- ggdraw() + draw_image(plot3)

# Combine the images in a single row or arrange as desired
combined_plot <- plot_grid(
  img1, img3, img2,
  ncol = 1,
  labels = c("A", "B", "C"),
  label_size = 16,          # Adjust label size
  label_x = 0.1,            # Adjust horizontal position of labels
  label_fontface = "bold",
  label_fontfamily = "Times"
  );combined_plot

ggsave(filename = file.path(plot_dir, "plotly_example.jpeg"),width = 8, height = 5, dpi = 600)